﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MMOG
{
    public partial class GameInterface3d : Form
    {
        int lx, ly;

        public GameInterface3d()
        {
            InitializeComponent();
            OpenGLControl.glHint(OpenGLControl.GL_PERSPECTIVE_CORRECTION_HINT, OpenGLControl.GL_NICEST);
            OpenGLControl.glPixelStorei(OpenGLControl.GL_UNPACK_ALIGNMENT, 1);
            OpenGLControl.glTexParameteri(OpenGLControl.GL_TEXTURE_2D, OpenGLControl.GL_TEXTURE_WRAP_S, OpenGLControl.GL_REPEAT);
            OpenGLControl.glTexParameteri(OpenGLControl.GL_TEXTURE_2D, OpenGLControl.GL_TEXTURE_WRAP_T, OpenGLControl.GL_REPEAT);
            OpenGLControl.glTexParameteri(OpenGLControl.GL_TEXTURE_2D, OpenGLControl.GL_TEXTURE_MAG_FILTER, OpenGLControl.GL_LINEAR_MIPMAP_LINEAR);
            OpenGLControl.glTexParameteri(OpenGLControl.GL_TEXTURE_2D, OpenGLControl.GL_TEXTURE_MIN_FILTER, OpenGLControl.GL_LINEAR_MIPMAP_LINEAR);
            OpenGLControl.glTexEnvf(OpenGLControl.GL_TEXTURE_ENV, OpenGLControl.GL_TEXTURE_ENV_MODE, OpenGLControl.GL_DECAL);

            OpenGLControl.glMatrixMode(OpenGLControl.GL_PROJECTION);
            OpenGLControl.glLoadIdentity();
            OpenGLControl.glFrustum(-1, 1, -1, 1, 2.5f, 20f);
            OpenGLControl.glMatrixMode(OpenGLControl.GL_MODELVIEW);
            OpenGLControl.glLoadIdentity();

            OpenGLControl.glViewport(0, 0, glControl.Width, glControl.Height);

            OpenGLControl.glEnable(OpenGLControl.GL_DEPTH_TEST);
            OpenGLControl.glEnable(OpenGLControl.GL_TEXTURE_2D);
            OpenGLControl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

        }
                
        private void glControl_KeyDown(object sender, KeyEventArgs e)
        {
            Client.KeyStatus[e.KeyValue] = true;
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    Client.exited = true;
                    break;
                case Keys.Up:
                    Client.board.moveUp();
                    break;
                case Keys.Down:
                    Client.board.moveDown();
                    break;
                case Keys.Left:
                    Client.board.moveLeft();
                    break;
                case Keys.Right:
                    Client.board.moveRight();
                    break;
                case Keys.Enter:
                    Client.board.select();
                    break;
            }

            Text = "key= " + e.KeyCode;
        }

        private void glControl_KeyUp(object sender, KeyEventArgs e)
        {
            Client.KeyStatus[e.KeyValue] = false;
        }

        private void fpsTimer_Tick(object sender, EventArgs e)
        {
            //Text = "Fps = " + Client.FramePerSec;
            Client.FramePerSec = 0;
        }

        private void glControl_MouseWheel(object sender, MouseEventArgs e)
        {
            Client.board.range -= e.Delta / 480f;
            if (Client.board.range < 1.25) Client.board.range = 1.25f;
            if (Client.board.range > 6) Client.board.range = 6f;
        }

        private void glControl_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                lx = e.X;
                ly = e.Y;
            }
        }

        private void glControl_MouseUp(object sender, MouseEventArgs e)
        {
        }

        private void glControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                int dx = e.X - lx;
                int dy = e.Y - ly;
                Client.board.xzAngle -= 180 * dx / glControl.Width;
                Client.board.yzAngle += 180 * dy / glControl.Height;
                if (Client.board.xzAngle > 45) Client.board.xzAngle = 45;
                if (Client.board.xzAngle < -45) Client.board.xzAngle = -45;
                if (Client.board.yzAngle > 90) Client.board.yzAngle = 90;
                if (Client.board.yzAngle < 0) Client.board.yzAngle = 0;
                lx = e.X;
                ly = e.Y;
            }
        }

    }
}
